class Sold < ActiveRecord::Base
  attr_accessible :stock_id, :cost, :quantity, :ref_id,:commission_percentage,:tax_percentage
  validates :cost, :numericality => {:greater_than => 0 },:presence => true
  validates :quantity, :numericality => { :only_integer => true, :greater_than => -1 },:presence => true
  validates :commission_percentage, :numericality => {:greater_than => -0.000001 },:presence => true
  validates :tax_percentage, :numericality => { :greater_than => -0.0000001 },:presence => true

  def self.createNew(ref_id,stock_id)
    @stock = Stock.find(stock_id)
    @stock.quantity = @stock.quantity-1
    @stock.save
    Sold.new(:ref_id=>ref_id,:stock_id=>stock_id,:cost=>@stock.sale_cost,:quantity=> 1)
  end

  def commission()
    quantity*cost*commission_percentage/100
  end

  def tax()
    quantity*cost*tax_percentage/100
  end

  def total()
    quantity*cost
  end

  def stock()
    Stock.find(stock_id)
  end

end
